<?php

namespace App\Http\Controllers\api;

use App\Http\Controllers\Controller;
use App\Http\Service\api\stateSecret;
use App\Http\Service\QRcodeService;
use App\Http\Service\Redisd;
use App\Models\MemberEquity;
use App\Models\MemberSiberian;
use App\Models\MemberType;
use App\Models\UserLevel;
use Illuminate\Http\Request;

class MemberController extends Controller
{
    public function siberianList(){
        $data = MemberSiberian::all();
        return ['data'=>$data,'msg'=>'卡密列表','code'=>200];
    }
    /**
     * 前端会员页面
     */
    public function memberUser(Request $request){
        $uid = $request->input('uid');
//        所有会员等级展示
        $memberEquity = MemberEquity::all()->toArray();
        $member = UserLevel::where('uid',$uid)->first();
//        用户会员等级
        $userLevel = $member->grade;
        $type = MemberType::where('id',$userLevel)->first();
        $userLevel = $type->member_name;
//        用户累计充值
        if (empty(Redisd::getInstance()->get('memberTop'.$uid))){
            $money = 0;
        }else{
            $money = Redisd::getInstance()->get('memberTop'.$uid);
        }
        if ($member->is_forever==1){
            $time = '永久';
        }else{
            $time = date('Y年m月d日 H:i:s',$member->valid_time);
        }
        return ['data'=>['memberEquity'=>$memberEquity,'userLevel'=>$userLevel,'time'=>$time,'money'=>$money],'msg'=>'会员页面展示','code'=>200];
    }

    /**
     * 会员充值接口
     * @param Request $request
     */
    public function memberTop(Request $request){
        $uid = $request->input('uid');
//        充值金额
        $number = $request->input('number');
        if (Redisd::getInstance()->get('memberTop'.$uid)+$number>=10){
            $grade = 1;
            Redisd::getInstance()->set('memberTop'.$uid,Redisd::getInstance()->get('memberTop'.$uid)+$number);
        }
        if (Redisd::getInstance()->get('memberTop'.$uid)+$number>=50){
            $grade = 2;
            Redisd::getInstance()->set('memberTop'.$uid,Redisd::getInstance()->get('memberTop'.$uid)+$number);
        }
        if (Redisd::getInstance()->get('memberTop'.$uid)+$number>=100){
            $grade = 3;
            Redisd::getInstance()->set('memberTop'.$uid,Redisd::getInstance()->get('memberTop'.$uid)+$number);
        }
        if (Redisd::getInstance()->get('memberTop'.$uid)+$number<10){
            $grade = 0;
            Redisd::getInstance()->set('memberTop'.$uid,Redisd::getInstance()->get('memberTop'.$uid)+$number);
        }
        if ($grade>0){
            $is_forever = 1;
        }else{
            $is_forever = 0;
        }
        $array = [
            'uid'=>$uid,
            'level_id'=>$grade,
            'valid_time'=>0,
            'is_forever'=>$is_forever,
            'mer_id'=>0,
            'status'=>1,
            'mark'=>'永久会员',
            'remind'=>0,
            'is_del'=>0,
            'add_time'=>time(),
            'discount'=>9
        ];
        $res = UserLevel::create($array);
//        会员记录
        if ($res){
            return ['msg'=>'充值成功','data'=>'','code'=>200];
        }
        return ['msg'=>'充值失败','data'=>'','code'=>400];
    }

    /**
     * 国密算法
     */
    public function guoMi(){
        $sm4 = new stateSecret();
        //内容
        $original_plain_text_data = '王术龙';
        //秘钥（16位字符串进行十六进制转换之后,作为秘钥*需为数字）
        $new_key = '74677467746774677467746774677467';

        $encrypted_text_data = $sm4->setKey($new_key)->encryptData(($original_plain_text_data)); // 加密字符串
        $two = $sm4->lock_url($encrypted_text_data,$new_key);
        echo '加密后的数据: '. $two;
//        echo '加密后的数据: '. $encrypted_text_data;
        $decrypted_text_data = $sm4->setKey($new_key)->decryptData(($encrypted_text_data));
        echo '解密后的数据: '. ($decrypted_text_data);
    }

    /**
     * 领取卡密*
     */
    public function getSiberian(Request $request){
        $uid = $request->input('uid');
        $name = $request->input('name');
        $level = UserLevel::where('uid',$uid)->where('is_del',0)->first();
        if ($level->is_forever==1){
            return ['msg'=>'会员不能领取','code'=>'','data'=>''];
        }else{
            $data = Redisd::getInstance()->get($name.'Siberian');
            $item = MemberSiberian::where('name',$name)->first();
            if ($item->status=='false'){
                return ['msg'=>'该卡密暂时不能领取','code'=>'','data'=>''];
            }
            if (empty($data)){
                return ['msg'=>'该卡密已被领完','code'=>'','data'=>''];
            }
            $siberian = array_pop($data);
            Redisd::getInstance()->set($name.'Siberian',json_encode($data));
            Redisd::getInstance()->set($uid.$siberian.$name,$siberian);
//        取出卡密
            return ['msg'=>'该卡密领取成功','code'=>'','data'=>$siberian];
        }
    }

    /**
     * 认证卡密*
     */
    public function accreditedSiberian(Request $request){
        $uid = $request->input('uid');
        $siberian = $request->input('siberian');
        $name = $request->input('name');
        if (Redisd::getInstance()->get($uid.$siberian.$name)==$siberian){
            $item = MemberSiberian::where('name',$name)->first();
            if (empty($item)){
                return ['msg'=>'卡密名称不存在','data'=>'','code'=>''];
            }
            if (empty(UserLevel::where('uid',$uid)->where('is_del',0)->first())){
                $time = strtotime('+'.$item->day);
                MemberSiberian::where('name',$name)->update(['use_number'=>$item->use_number+1]);
                $array=[
                    'uid'=>$uid,
//                    'level_id'=>1,
                    'grade'=>1,
                    'valid_time'=>$time,
                    'is_forever'=>0,
                    'mer_id'=>0,
                    'status'=>1,
                    'mark'=>$name.'卡密会员',
                    'remind'=>0,
                    'is_del'=>0,
                    'add_time'=>time(),
                    'discount'=>9
                ];
                UserLevel::create($array);
                return ['msg'=>'卡密会员激活成功，新用户注册','data'=>'','code'=>200];
            }
            $user = UserLevel::where('uid',$uid)->first();
            if ($user->valid_time<=time()){
                $time = strtotime('+'.$item->day);
                MemberSiberian::where('name',$name)->update(['use_number'=>$item->use_number+1]);
                UserLevel::where('uid',$uid)->update(['grade'=>1,'valid_time'=>$time]);
                Redisd::getInstance()->del($uid.$siberian.$name);
                return ['msg'=>'卡密会员激活成功，完结续费','data'=>'','code'=>200];
            }
            UserLevel::where('uid',$uid)->update(['valid_time'=>$user->valid_time+86400]);
            MemberSiberian::where('name',$name)->update(['use_number'=>$item->use_number+1]);
            Redisd::getInstance()->del($uid.$siberian.$name);
            return ['msg'=>'卡密会员激活成功，未完结续费','data'=>'','code'=>200];
        }
        return ['msg'=>'卡密错误','data'=>'','code'=>'500'];
    }

    /**
     * 检测会员时间是否过期
     */
    public static function memberTimeTest(){
        $data = UserLevel::where('is_del',0)->where('is_forever',0)->get()->toArray();
        foreach ($data as $key => $val){
            if ($val['valid_time']<=time()){
//                过期
                UserLevel::where('uid',$val['uid'])->update(['is_del'=>1]);
            }
        }
    }

    /**
     * 永久会员降级机制
     */
    public static function memberDerated(){
        $data = UserLevel::where('is_del',0)->where('is_forever',1)->get()->toArray();
        foreach ($data as $key => $val){
            if (time()-$val['add_time']>=2592000){
                if ($val['grade']>=1){
                    UserLevel::where('uid',$val['uid'])->update(['grade'=>$val['grade']-1]);
                }
            }
        }
    }
}
